App.Taskbar = new Ext.Toolbar({
	region: 'center'
	,ctCls: 'taskbar'
	,cls: 'taskbar'
	,winIdPrefix: 'tb-bt-'
	,wins: {}
	,getAllWin: function(){
		return this.wins;	
	}
	,addWin: function(win){
		var id = this.winIdPrefix + win.id;
		if(this.wins[id]) return this.wins[id];
		this.wins[id] = win;
		
		var btConf = {
			id: id
			,text: win.title.truncate(15)
			,cls: 'button'
			,overCls: 'hover'
			,iconCls: win.iconCls
			,height: 30
			,listeners: {
				click: function(bt){
					if(App.WindowManager.getActive() == win) win.hide(bt.id);
					else win.show(bt.id);
				}
				,render: function(bt){
					bt.getEl().on('contextmenu', function(e, t, o){
						bt.showContextMenu(e.getXY());
						e.stopEvent();
					});
					
					App.Taskbar.on('add', function(taskbar){
						this.getContextMenu().closeAllBt.setDisabled(taskbar.wins.length > 1);
					}, this);
					App.Taskbar.on('removed', function(taskbar){
						this.getContextMenu().closeAllBt.setDisabled(taskbar.wins.length > 1);
					}, this);
				}
			}
			,getContextMenu: function(){
				if(this.contextmenu) return this.contextmenu;
				this.contextmenu = new Ext.menu.Menu({
					cls: 'start-menu'
					,items: [
						{
							text: 'Show'
							,disabled: true
							,ref: 'showBt'
							,handler: function(){
								win.show();	
							}
						},{
							text: 'Hide'
							,ref: 'hideBt'
							,handler: function(){
								win.hide();
							}
						},'-',{
							text: 'Close'
							,ref: 'closeBt'
							,handler: function(){
								win.close();
							}	
						},{
							text: 'Close Others'
							,ref: 'closeAllBt'
							,handler: function(){
								for(var i in App.Taskbar.wins){
									if(App.Taskbar.wins[i] != win)
										App.Taskbar.wins[i].close();
								};
							}
						}
					]
					,listeners: {
						show: function(menu){
							menu.closeAllBt.setDisabled(App.Taskbar.items.length <= 1);
						}
					}	
				});
				
				win.on('show', function(win){
					this.contextmenu.showBt.disable();
					this.contextmenu.hideBt.enable();
				}, this);
				win.on('hide', function(win){
					this.contextmenu.showBt.enable();
					this.contextmenu.hideBt.disable();
				}, this);
				
				return this.contextmenu;
			}
			,showContextMenu: function(at){
				this.getContextMenu().showAt(at);
			}
		};
		
		this.addButton(btConf);
		
		this.doLayout();
		return win;
	}
	,removeWin: function(win){
		var id = this.winIdPrefix + win.id;
		this.remove(id);
		delete this.wins[id];
		return this;
	}
	,listeners: {
		render: function(panel){
			panel.getEl().on('contextmenu', function(e, t, o){
				panel.showContextMenu(e.getXY());
				e.stopEvent();	
			});
		}	
	}
	,getContextMenu: function(){
		if(this.contextmenu) return this.contextmenu;
		
		this.contextmenu = new Ext.menu.Menu({
			cls: 'start-menu'
			,items: [
				{
					text: 'Show All'
					,handler: function(){
						for(var i in App.Taskbar.wins){
							App.Taskbar.wins[i].show();
						};
					}
				},{
					text: "Hide All"
					,handler: function(){
						for(var i in App.Taskbar.wins){
							App.Taskbar.wins[i].hide();
						};
					}
				},'-',{
					text: "Close All"
					,handler: function(){
						for(var i in App.Taskbar.wins){
							App.Taskbar.wins[i].close();
						};
					}	
				}
			]	
		});
		return this.contextmenu;
	}
	,showContextMenu: function(at){
		this.getContextMenu().showAt(at);
	}
});